package com.wulinbus.controller.admin;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fancy.common.utils.StringUtil;
import com.wulinbus.model.CreativeHelperModel;
import com.wulinbus.model.RenderBannerModel;
import com.wulinbus.model.RenderProviderModel;
import com.wulinbus.service.RenderBannerService;

@Controller
@RequestMapping("/admin/render_banner")
public class AdminRenderBannerController {
	@Resource
	RenderBannerService renderBannerService;
	/**
	 * 渲染焦点图列表
	 */
	@RequestMapping("render_banner_list.html")
	public String render_banner_list(@CookieValue(value="muid",defaultValue="") String muid,Model model,HttpServletRequest request) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{ 
			List<RenderBannerModel> renderBannerList=renderBannerService.getAllRenderBanner();
			model.addAttribute("renderBanner", renderBannerList);
			return "admin/render_banner_list";
		}
	}
	/**
	 * 渲染焦点图编辑页面显示
	 */
	@RequestMapping("edit_display.html")
	public String edit_display(Integer rbid,@CookieValue(value="muid",defaultValue="") String muid,Model model,HttpServletRequest request) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{ 
			RenderBannerModel renderBannerModel=renderBannerService.getRenderBannerModel(rbid);
			model.addAttribute("renderBannerModel", renderBannerModel);
			return "admin/render_banner_edit";
		}
	}
	/**
	 * 渲染焦点图编辑
	 * @throws IOException 
	 */
	@RequestMapping("edit.json")
	public void edit(Integer rbid,RenderBannerModel renderBannerModel,@CookieValue(value="muid",defaultValue="") String muid,Model model,HttpServletRequest request,HttpServletResponse response) throws IOException {
			response.setContentType("application/json; charset=utf-8");
			renderBannerModel.setRbid(rbid);
			renderBannerService.update(renderBannerModel);
			response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
	}
	/**
	 * 渲染焦点图添加页面显示
	 */
	@RequestMapping("add_display.html")
	public String add_display(@CookieValue(value="muid",defaultValue="") String muid,Model model,HttpServletRequest request) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{ 
			return "admin/render_banner_add";
		}
	}
	/**
	 * 渲染焦点图添加
	 * @throws IOException 
	 */
	@RequestMapping("add.json")
	public void add(RenderBannerModel renderBannerModel,@CookieValue(value="muid",defaultValue="") String muid,Model model,HttpServletRequest request,HttpServletResponse response) throws IOException {
			response.setContentType("application/json; charset=utf-8");
			renderBannerService.insert(renderBannerModel);
			response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
	}
	/**
	 * 渲染焦点图删除
	 * @throws IOException 
	 */
	@RequestMapping("delete.json")
	public void delete(Integer rbid,@CookieValue(value="muid",defaultValue="") String muid,Model model,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
			if(renderBannerService.delete(rbid)>0)
			{
				response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
			}
			else
			{
				response.getWriter().print("{\"message\":\"操作失败\",\"statusCode\":\"300\"}");
			}
	}
}
